package lumis.portal;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;

import lumis.portal.manager.*;
import lumis.portal.stability.StableMinor;
import lumis.util.IResource;
import lumis.util.XmlUtil;
import lumis.util.log.ILogger;
import lumis.util.log.LoggerFactory;

/**
 * Base exception for the portal framework. Adds localization functionalities,
 * based on {@link lumis.portal.localization.ILocalizationManager}.
 * 
 * @since 4.0.0
 * @version $Revision: 13082 $ $Date: 2011-05-27 16:03:12 -0300 (Fri, 27 May 2011) $
 */
@StableMinor(version = "6.0", sinceVersion = "4.0")
public class PortalException extends Exception
{
	private static final long serialVersionUID = 1;

	private static ILogger logger = LoggerFactory.getLogger(PortalException.class);
	protected List<IResource> resources = null;
	
	public PortalException(String arg0)
	{
		this(arg0, (List<IResource>)null, null);
	}
	
	public PortalException(String arg0, Exception innerException)
	{
		this(arg0, (List<IResource>)null, innerException);
	}

	public PortalException(String arg0, IResource resource)
	{
		this(arg0, resource, null);
	}

	public PortalException(String arg0, List<IResource> resources)
	{
		this(arg0, resources, null);
	}

	public PortalException(String arg0, IResource resource, Exception innerException)
	{
		this(arg0, Arrays.asList(resource), innerException);
	}

	public PortalException(String arg0, List<IResource> resources, Exception innerException)
	{
		super(arg0, innerException);
		if (resources != null)
			this.resources = resources;
		else
			this.resources = Arrays.<IResource>asList(new PortalStringResource());
		logExceptionCreation();
	}
	
	protected void logExceptionCreation()
	{
		if (logger.isDebugEnabled())
			logger.debug(this.getClass().getName() + " created", this);				
	}
	
	public String getLocalizedMessage(Locale locale)
	{
		try
		{
			return ManagerFactory.getLocalizationManager().localize(getMessage(), resources, locale);
		}
		catch (Exception e)
		{
			return getMessage();
		}
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Throwable#getLocalizedMessage()
	 */
	public String getLocalizedMessage()
	{
		try
		{
			return ManagerFactory.getLocalizationManager().localize(getMessage(), resources);
		}
		catch (Exception e)
		{
			return getMessage();
		}
	}

	public List<IResource> getResources()
	{
		return resources;
	}
	
	public String toXmlString(Locale locale)
	{
		if(locale == null)
			locale = PortalContext.getDefaultLocale();
		
	    String className = getClass().getName();
		String classSimpleName = className.substring(className.lastIndexOf(".")+1); // strip the package name
		return 
			"<PortalException" +
			" type=\"" + classSimpleName + "\">" +
			"<message>" + XmlUtil.encodeXml(getLocalizedMessage(locale)) + "</message>" +
			"</PortalException>";
	}
	
	@Override
	public String toString()
	{
        String className = getClass().getName();
		String message = getMessage();
		return (message != null) ? (className + ": " + message) : className;
	}
}
